package se.webbzon.boltzmann.editor.factory.propertyframe;

import java.awt.GridLayout;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import se.webbzon.boltzmann.editor.component.sequence.SequenceBuilderPanel;
import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.boltzmann.editor.propertyframe.WorldObjectPropertyFrame;
import se.webbzon.boltzmann.game.object.trigger.SequenceTrigger;
import se.webbzon.boltzmann.game.object.trigger.TriggerCondition;
import se.webbzon.oschi01.worldobject.WorldObject;

public class SequenceTriggerPropertyFrameFactory implements PropertyFrameFactory {

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public String getWorldObjectName() {
		return SequenceTrigger.worldObjectName;
	}

	@Override public WorldObjectPropertyFrame makePropertyFrame(EditorModel model,
																WorldObject instance) {
		return new PropertyFrameImpl(model, (SequenceTrigger) instance);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private static final class PropertyFrameImpl extends WorldObjectPropertyFrame {
		
		// The combo boxes of this property frame
		private final JComboBox<TriggerCondition> conditionInput;
		
		// The check boxes of this property frame
		private final JCheckBox blockingInput;
		
		// The sequence builder panel of this property frame
		private final SequenceBuilderPanel sequencePanel;
		
		/*============================================================
		Constructors
		============================================================*/
		
		public PropertyFrameImpl(EditorModel model, SequenceTrigger trigger) {
			super(model, trigger);
			
			// Create trigger panel
			JPanel triggerPanel = new JPanel();
			triggerPanel.setLayout(new BoxLayout(triggerPanel,BoxLayout.Y_AXIS));
			
			// Create configuration panel
			JPanel configPanel = new JPanel();
			configPanel.setBorder(BorderFactory.createTitledBorder("Trigger Configuration:"));
			configPanel.setLayout(new GridLayout(2,2));
			
			configPanel.add(new JLabel("Trigger Condition:"));
			conditionInput = new JComboBox<TriggerCondition>(TriggerCondition.values());
			conditionInput.setSelectedItem(trigger.getCondition());
			configPanel.add(conditionInput);
			configPanel.add(new JLabel("Player Blocking:"));
			blockingInput = new JCheckBox("",trigger.isBlocking());
			configPanel.add(blockingInput);
			triggerPanel.add(configPanel);
			
			addPanel("Trigger",triggerPanel);
			
			// Create sequence panel
			sequencePanel = new SequenceBuilderPanel(model,trigger.getBuilder());
			addPanel(sequencePanel.getName(),sequencePanel);
			
			initialize();
		}
		
		/*============================================================
		Public Methods
		============================================================*/

		@Override public boolean apply(Collection<String> errorMessages) {
			SequenceTrigger trigger = (SequenceTrigger) getInstance();
			trigger.setCondition((TriggerCondition) conditionInput.getSelectedItem());
			trigger.setBlocking(blockingInput.isSelected());
			return true;
		}
	}

}
